package udp.client.test;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

import android.util.Log;

public class Client implements Runnable {
	public String SERVERIP = "127.0.0.1";
	public int SERVERPORT = 4444;
	public DatagramSocket clientSocket;
	
@Override
	public void run() {
		try {
			// Retrieve the ServerName
			InetAddress serverAddr = InetAddress.getByName(SERVERIP);

			Log.d("UDP", "C: Connecting...");
			/* Create new UDP-Socket */
			clientSocket = new DatagramSocket();

			/* Prepare some data to be sent. */
			String easySudoku =
				"360000000004230800000004200" +
				"070460003820000014500013020" +
				"001900000007048300000000045" ;
			
			for (int i = 0; i < 10; i++) {
				byte[] buf = (easySudoku).getBytes();

				/*
				 * Create UDP-packet with data & destination(url+port)
				 */
				DatagramPacket packet = new DatagramPacket(buf, buf.length,
						serverAddr, SERVERPORT);
				Log.d("UDP", "C: Sending: '" + new String(buf) + "'");

				/* Send out the packet */

				synchronized (this) {
					  this.wait(500);
					}
				clientSocket.send(packet);
				Log.d("UDP", "C: Sent.");
				Log.d("UDP", "C: Recieving packet...");
				clientSocket.receive(packet);
				Log.d("UDP", "C: Received: '" + new String(packet.getData())
						+ "'");
				Log.d("UDP", "C: Done.");
			}
			clientSocket.close();
			
		} catch (Exception e) {
			Log.e("UDP", "C: Error", e);
		}
	}
}